package com.zxq.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxq.exchange.model.ExchangeDTO;
import com.zxq.factory.annotate.DecoderField;
import com.zxq.web.dto.PlatformCmdDTO;
import com.zxq.web.entity.IotCmdDecodeDo;
import com.zxq.web.entity.IotCmdDo;
import com.zxq.web.entity.IotDeviceDo;
import com.zxq.web.service.ITransformService;
import com.zxq.web.service.IotCmdEncoderDoService;
import com.zxq.web.service.IotProductDoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ITransformServiceImpl implements ITransformService {

    /**
     * 设备指令解码
     */
    @Autowired
    IotCmdDecodeDoServiceImpl iotCmdDecodeDoService;

    /**
     * 平台指令编码
     */
    @Autowired
    IotCmdEncoderDoService iotCmdEncoderDoService;

    /**
     * 产品服务
     */
    @Autowired
    IotDeviceDoServiceImpl iotDeviceDoService;

    /**
     * 产品服务
     */
    @Autowired
    IotProductDoService iotProductDoService;

    /**
     * cmd服务
     */
    @Autowired
    IotCmdDoServiceImpl cmdDoService;





    @Override
    public ExchangeDTO platTransform(PlatformCmdDTO platformCmdDTO) {
        Long devId =  platformCmdDTO.getDevId();
        String cmdHexId =  platformCmdDTO.getCmdHexId();
        IotDeviceDo iotDeviceDo = iotDeviceDoService.getById(devId);
        if(iotDeviceDo == null) {
            log.info(String.format("devId:{0} not belong product!",devId));
            return null;
        }
        Long productId = iotDeviceDo.getProductId();
        Long packetId = iotProductDoService.getById(productId).getPacketId();
        IotCmdDo iotCmdCondition = new IotCmdDo();
        iotCmdCondition.setPacketId(packetId);
        iotCmdCondition.setCmdHexId(cmdHexId);
        QueryWrapper<IotCmdDo> queryWrapper = new QueryWrapper<>(iotCmdCondition);
        IotCmdDo iotCmdDo = cmdDoService.getOne(queryWrapper);
        Long cmd_id =  iotCmdDo.getId();
        IotCmdDecodeDo iotCmdDecodeDo = new IotCmdDecodeDo();
        iotCmdDecodeDo.setCmdId(cmd_id);
        QueryWrapper<IotCmdDecodeDo> iotCmdDecodeDoQueryWrapper = new QueryWrapper<>(iotCmdDecodeDo);
        List<IotCmdDecodeDo> iotCmdDecodeDos = iotCmdDecodeDoService.list(iotCmdDecodeDoQueryWrapper);
        iotCmdDecodeDos.sort(((o1, o2) -> {
            return o1.getSequence() - o2.getSequence();
        }));




        return null;
    }


    @Override
    public PlatformCmdDTO devTransform(ExchangeDTO exchangeDTO) {

        return null;
    }






}
